package org.ben.birthday.twentybirthday.service.impl;

import org.ben.birthday.twentybirthday.dao.BackGroundMusicDaoRepository;
import org.ben.birthday.twentybirthday.entity.BackgroundMusic;
import org.ben.birthday.twentybirthday.service.IBackGroundMusicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @Author: Ben
 * @Date: 2019/8/8 14:08
 */
@Service
public class BackGroundMusicImpl implements IBackGroundMusicService {

    @Autowired
    BackGroundMusicDaoRepository backGroundMusicDaoRepository;

    private static final Logger logger = LoggerFactory.getLogger(BackGroundMusicImpl.class);

    /**
     * 增加背景音乐Service
     * @param backgroundMusic
     * @return boolean
     */
    @Override
    public boolean insertBackgroundMusicService(BackgroundMusic backgroundMusic) {
        List<BackgroundMusic> backgroundMusicList=backGroundMusicDaoRepository.findBackgroundMusicByMusicUrl(backgroundMusic.getMusicUrl());
        Iterator<BackgroundMusic> backgroundMusicIterator=backgroundMusicList.iterator();
        while(backgroundMusicIterator.hasNext()){
            logger.error("该背景音乐已存在，增加失败");
            return false;
        }
        backGroundMusicDaoRepository.save(backgroundMusic);
        logger.info("背景音乐添加成功");
        return true;
    }

    /**
     * 删除背景音乐Service
     * @param backGroundMusicId
     * @param musicUrl
     * @return boolean
     */
    @Override
    public boolean deleteBackgroundMusicService(Integer backGroundMusicId, String musicUrl) {
        List<BackgroundMusic> backgroundMusicList=backGroundMusicDaoRepository.findBackgroundMusicByMusicUrl(musicUrl);
        Iterator<BackgroundMusic> backgroundMusicIterator=backgroundMusicList.iterator();
        while (backgroundMusicIterator.hasNext()){
            backGroundMusicDaoRepository.deleteById(backGroundMusicId);
            logger.info("该背景音乐删除成功");
            return true;
        }
        logger.error("该背景音乐不存在，删除失败");
        return false;
    }

    /**
     * 更改背景音乐Service
     * @param backgroundMusic
     * @return boolean
     */
    @Override
    public boolean updateBackgroundMusicService(BackgroundMusic backgroundMusic) {
        List<BackgroundMusic> backgroundMusicList=backGroundMusicDaoRepository.findBackgroundMusicByMusicUrl(backgroundMusic.getMusicUrl());
        Iterator<BackgroundMusic> backgroundMusicIterator=backgroundMusicList.iterator();
        while (backgroundMusicIterator.hasNext()){
            backGroundMusicDaoRepository.saveAndFlush(backgroundMusic);
            logger.info("背景音乐更改成功");
            return true;
        }
        logger.error("查无此背景音乐，更改失败");
        return false;
    }

    /**
     * 根据backGroundMusicId查询背景音乐信息Service
     * @param backGroundMusicId
     * @return BackgroundMusic
     */
    @Override
    public BackgroundMusic selectBackgroundMusicService(Integer backGroundMusicId) {
        Optional<BackgroundMusic> optionalBackgroundMusic=backGroundMusicDaoRepository.findById(backGroundMusicId);
        boolean judgment=optionalBackgroundMusic.isPresent();
       if (judgment){
           BackgroundMusic backgroundMusic=optionalBackgroundMusic.get();
           logger.info(backgroundMusic.toString());
           return backgroundMusic;
       }else {
           logger.error("您查询的音乐不存在，请重新查询");
           return null;
       }
    }
}
